
	

/**
 * 匀速动画
 * @param {位移元素} elm 
 * @param {目标位置} target 
 */
function moveAnimation(elm, target) {
    clearInterval(elm.timeID);
    let position = elm.offsetLeft;
    let step = (target - position) > 0 ? 10 : -10;
    elm.timeID = setInterval(
        function() {
            position += step;
            if (Math.abs(target - position) > Math.abs(step)) {
                elm.style.left = position + 'vh';
            } else {
                elm.style.left = target + 'vh';
                clearInterval(elm.timeID);
            }
        }, 20
    )
}


/**
 * 描述 减速运动 缓动
 * @date 2020-04-05
 * @param {位移元素} elm
 * @param {目标位置} target
 * @returns {any}
 */
function slowAnimation(elm, target) {
    clearInterval(elm.timeID);
    let position = elm.offsetLeft;
    elm.timeID = setInterval(
        function() {
            let step = position > target ? Math.floor((target - position) / 10) : Math.ceil((target - position) / 10);
            position += step;
            if (Math.abs(target - position) > Math.abs(step)) {
                elm.style.left = position + 'px';
            } else {
                elm.style.left = target + 'px';
                clearInterval(elm.timeID);
            }
        }, 30
    )
}

/**
 * 描述 加速运动
 * @date 2020-04-05
 * @param {位移元素} elm
 * @param {目标位置} target
 * @returns {any}
 */
function rapidAnimation(elm, target) {
    clearInterval(elm.timeID);
    let position = elm.offsetLeft;
    elm.timeID = setInterval(
        function() {
            let step = position > target ? Math.floor(1000 / (target - position)) : Math.ceil(1000 / (target - position));
            position += step;
            if (Math.abs(target - position) > Math.abs(step)) {
                elm.style.left = position + 'px';
            } else {
                elm.style.left = target + 'px';
                clearInterval(elm.timeID);
            }
        }, 10
    )
}


/*获取的所有样式的兼容性写法，不管你是什么都能够获取该样式
 * 
 */
function  getStyle(obj, name)  {
    if (obj.currentStyle) {    //cuyreStye是IE的兼容性写法 
        return  obj.currentStyle[name];
    } else {    
        return  getComputedStyle(obj, false)[name];
    }
}

/**
 * 
 * @param {变换的元素} elm 
 * @param {变换的属性集合} obj 
 * @param {回调函数} fun 
 */
function Animation(elm, obj, fun) {
    clearInterval(elm.timeID);
    elm.timeID = setInterval(
        function() {
            let flag = true;

            for (let key in obj) {
                if (key == 'z-index') {
                    // 直接赋值即可
                    elm.style[key] = parseInt(obj[key]);
                } else if (key == 'opacity') {

                    let position = getStyle(elm, key) * 100; // 将原本的值变大一百倍

                    let step = (obj[key] * 100 - position) / 10; // 将目标值变大一百倍
                    step = step > 0 ? Math.ceil(step) : Math.floor(step);
                    position += step;
        
                    elm.style[key] = position / 100; // 将结果还原 / 100,去掉单位
                    if (position != obj[key] * 100) {
                        flag = false;
                    }
                } else {
                    let position = parseInt(getStyle(elm, key));
                    let step = (obj[key] - position) / 10;
                    step = step > 0 ? Math.ceil(step) : Math.floor(step);
                    position += step;
                 
                    elm.style[key] = position + 'px';
                    if (position != obj[key]) {
                        flag = false;
                    }
                }
            }
            if (flag) {
                clearInterval(elm.timeID);
                if (Object.prototype.toString.call(fun) == '[object Function]') {
                    fun();
                }

            }
        }, 20
    )
}

module.exports = {
	Animation,
	getStyle
}